<?php

namespace app\api\controller;

use app\common\Helper;
use app\admin\model\Storegoods as StoregoodsMdl;

use think\Db;
use think\Exception;

class Storegoods extends Base
{

    /**
     * 商品展示
     */
    public function getStoregoods(){
        $param = self::getHttpParam();

        $start = 0;
        $length = 20;
        if (empty($param->storeId)) {
            return show(config('status.error_code'), self::NOT_PARAM, 'storeId 不能为空');
        }
        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $where = [
            'storeId' => $param->storeId
        ];

        $model = new StoregoodsMdl();
        $fields = 'id, storeId, title, imgUrl';
        $result = $model->where($where)->field($fields)->limit($start, $length)->order('createDate DESC')->select();
        $total = $model->where($where)->count();

        if (empty($result)) {
            return show(config('status.error_code'), self::NOT_DATA, '未查到商品');
        }

        return showTotal(config('status.success_code'), self::MSG_SUCCESS, $result, $total);
    }

    /**
     * 保存商品
     */
    public function saveStoregoods(){
        $param = self::getHttpParam();

        if (empty($param->storeId)) {
            return show(config('status.error_code'), self::NOT_PARAM, 'storeId 不能为空');
        }
        if (empty($param->title)) {
            return show(config('status.error_code'), self::NOT_PARAM, 'title 商品名称不能为空');
        }
        if (empty($param->imgUrl)) {
            return show(config('status.error_code'), self::NOT_PARAM, 'imgUrl 商品图片不能为空');
        }

        $data = (array)$param;

        Db::startTrans();
        try {
            $model = new StoregoodsMdl();

            if (!empty($param->goodsId)) {
                // 修改
                $data['id'] = $param->goodsId;
                $data['updateDate'] = date('Y-m-d H:i:s');

                $result = $model->allowField(true)->save($data, ['id' => $data['id']]);
            } else {
                // 新增
                $data['id'] = Helper::getUUID();
                $data['createDate'] = date('Y-m-d H:i:s');

                $result = $model->save($data);
            }

            if($result !== false){
                Db::commit();
                return show(config('status.success_code'), self::MSG_SUCCESS, '保存成功');
            }else{
                Db::rollback();
                return show(config('status.error_code'), self::MSG_ERROR, $model->getError());
            }
        } catch (Exception $e) {
            Db::rollback();
            return show(config('status.error_code'), self::MSG_ERROR, $e->getMessage());
        }
    }

    /**
     * 删除商品
     */
    public function delStoregoods(){
        $param = self::getHttpParam();

        if (empty($param->goodsId)) {
            return show(config('status.error_code'), self::NOT_PARAM, 'goodsId 不能为空');
        }

        $where = [
            'id' => $param->goodsId
        ];
        $model = new StoregoodsMdl();
        $result = $model->where($where)->delete();

        if($result === false){
            return show(config('status.error_code'), '删除失败', '删除失败');
        }

        return show(config('status.success_code'), '删除成功', '删除成功');
    }

}